package io.renren.modules.onlinestudy.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.HttpRequest;
import io.renren.modules.onlinestudy.entity.*;
import io.renren.modules.sys.entity.SysDictEntity;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.onlinestudy.service.XcBmService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;


/**
 * 
 * 
 * @author zhubsh
 * @email sd_zhusbh@163.com
 * @date 2018-10-19 09:34:02
 */
@RestController
@RequestMapping("onlinestudy/xcbm")
public class XcBmController {
    private Logger log = LoggerFactory.getLogger(XcBmController.class);
    @Autowired
	private XcBmService xcBmService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("onlinestudy:xcbm:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<XcBmEntity> xcBmList = xcBmService.queryList(query);
		int total = xcBmService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(xcBmList, total, query.getLimit(), query.getCurrPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("onlinestudy:xcbm:info")
	public R info(@PathVariable("id") Integer id){
		XcBmEntity xcBm = xcBmService.queryObject(id);
		
		return R.ok().put("xcBm", xcBm);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("onlinestudy:xcbm:save")
	public R save(@RequestBody XcBmEntity xcBm){
		xcBmService.save(xcBm);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("onlinestudy:xcbm:update")
	public R update(@RequestBody XcBmEntity xcBm){
		xcBmService.update(xcBm);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("onlinestudy:xcbm:delete")
	public R delete(@RequestBody Integer[] ids){
		xcBmService.deleteBatch(ids);
		
		return R.ok();
	}

	@RequestMapping("/executeSh")
    @RequiresPermissions("onlinestudy:xcbm:executesh")
    public R executeSh(@RequestBody XcBmEntity xcBmEntity){
        xcBmService.executeSh(xcBmEntity);
        try{
            String userid = xcBmEntity.getUserid();
            //发送积分申请
            HttpRequest.sendPost("http://20.0.51.77:8082/dataservice/api/operatePoints.do", "userid=" + userid + "&type=8001");
        }catch(Exception e){
            log.error("在线学习审批赠积分失败");
        }
        return R.ok();
    }

    @RequestMapping("/showImages")
    public R showImages(@RequestBody XcBmEntity xcBmEntity){
        Map<String, Object> result_map = new HashedMap();
        //查询身份认证图片
        String avatart = xcBmService.queryXyxxAvatar(xcBmEntity);
        result_map.put("avatart", avatart);
        result_map.put("xy_id", xcBmEntity.getId());
        //学习日期列表
        List<String> imgDateList = xcBmService.queryXyxxImgDate(xcBmEntity);
        List<HashMap> resultList = new ArrayList<>();
        imgDateList.forEach((i)->{
            XyxxImage xyxxImage = new XyxxImage();
            xyxxImage.setXyId(xcBmEntity.getId());
            xyxxImage.setXxdate(i);
            List<XyxxImage> xyxxImages = xcBmService.queryXyxxImgs(xyxxImage);
            HashMap<String, Object> ldate_images_map = new HashMap<>();
            ldate_images_map.put("xxdate", i);
            ldate_images_map.put("imgList", xyxxImages);
            resultList.add(ldate_images_map);
        });
        result_map.put("resList", resultList);
        result_map.put("bmlx", xcBmEntity.getBmlx());
        return R.ok(result_map);
    }

    @RequestMapping("/xyxxShExtractImg")
    public R xyxxShExtractImg(@RequestBody XyxxSh xyxxsh) {
        xcBmService.updateImgarr(xyxxsh);
        return R.ok();
    }

    /**
     * 查看取样图片
     * @param xcBmEntity
     * @return
     */
    @RequestMapping("/showImgArr")
    public R showImgArr(@RequestBody XcBmEntity xcBmEntity){
        Map<String, Object> result_map = new HashedMap();
        XcBmEntity xyxx = xcBmService.queryObject(xcBmEntity.getId());
        result_map.put("xyxx", xyxx);

        List<XyxxImage> xyxxImages = xcBmService.selImgArrs(xyxx);
        result_map.put("imgList", xyxxImages);
        result_map.put("bmlx", xcBmEntity.getBmlx());
        return R.ok(result_map);
    }

    /**
     * 查询学员的学习地点
     * @return
     */
    @RequestMapping("/xxAddressList")
    public R xxAddressList(){
        List<SysDictEntity> studyAddresss = xcBmService.getStudyAddresss();
        Map<String, Object> result_map = new HashedMap();
        result_map.put("studyAddresss", studyAddresss);
        return R.ok(result_map);
    }

    /**
     * 学员学习情况统计
     * @param map
     * @return
     */
    @RequestMapping("/studyStatistics")
    public R getGroupStudyStatistics(@RequestParam Map<String, Object> map) {

        List<StudyStatistics> groupStudyStatistics = xcBmService.getGroupStudyStatistics(map);

        PageUtils pageUtils = new PageUtils(groupStudyStatistics, groupStudyStatistics.size(), 100, 1);

        return R.ok().put("page", pageUtils);
    }

    @RequestMapping("/statisticOnlineStudy")
    public R statisticOnlineStudy(){
        List<StudyStatistics> xxtj = xcBmService.getXxtj();
        PageUtils pageUtils = new PageUtils(xxtj, 2, 10, 1);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 列表
     */
    @RequestMapping("/xxVideoList")
    @RequiresPermissions("onlinestudy:xcvideo:list")
    public R xxVideoList(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<XcVideoEntity> xcVideoList = xcBmService.xxVideoList(query);
        int total = xcBmService.queryxxVideoTotal(query);

        PageUtils pageUtil = new PageUtils(xcVideoList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteVideos")
    @RequiresPermissions("onlinestudy:xcvideo:delete")
    public R deleteVideos(@RequestBody Integer[] ids){
        xcBmService.deleteVideosBatch(ids);
        return R.ok();
    }

    /**
     * 上传
     */
    @RequestMapping("/videoupload")
    @RequiresPermissions("onlinestudy:xcvideo:upload")
    public R videoupload(XcVideoEntity xcVideo){
        try {
            xcBmService.xxVideoSave(xcVideo);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok();
    }

}
